package com.two.screen.water.balance.print.auto_reply

import com.caysn.autoreplyprint.AutoReplyPrint
import com.sun.jna.Pointer
import com.sun.jna.WString
import com.two.screen.water.balance.net.api.DataApi
import com.two.screen.water.balance.print.bean.PrintConnectBean
import com.two.screen.water.balance.print.bean.PrintConnectSaleReportBean
import com.two.screen.water.balance.print.bean.PrintOrderBean
import com.two.screen.water.balance.print.bean.PrintServerCollectOrderBean
import com.two.screen.water.balance.utils.CommUtils
import com.two.screen.water.balance.utils.Params
import com.two.screen.water.balance.utils.SPUtils
import com.two.screen.water.balance.utils.isNotAvailable

object USBPrinterAutoReply {
    var mPointer: Pointer? = Pointer.NULL

    var mIsOpen = false

    var mDevicePath = "VID:0x4B43"

    fun initUsbDevice() {
        if (mIsOpen) return
        var devicePaths: Array<String>? = AutoReplyPrint.CP_Port_EnumUsb_Helper.EnumUsb();

        devicePaths?.forEach {
            d("devicePath::->$it")
            if (it.contains(mDevicePath)) {
                mPointer = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb(it, 1)
                mIsOpen = mPointer != Pointer.NULL
                DataApi.mUSBDevicePath = it
                SPUtils.putString(Params.mUSBDevicePath, it)
            }
        }
        d("devicePath::->mPointer：$mPointer")
    }

    fun d(msg: String) {
        CommUtils.println("USBPrinterAutoReply->", msg)
    }

    fun closePrinter() {
        try {
            if (mPointer != Pointer.NULL) {
                var isClose = AutoReplyPrint.INSTANCE.CP_Port_Close(mPointer)
                mPointer = Pointer.NULL
                d("关闭打印：$isClose")
            }
        } catch (e: Exception) {
            d("关闭打印失败：$e")
        }
    }

    fun printTest() {
        initUsbDevice()
        d("mPointer：$mPointer")
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()

        for (time in 1..times) {
            AutoReplyPrint.INSTANCE.CP_Printer_ClearPrinterBuffer(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_ResetPrinter(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteMode(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteEncoding(mPointer, AutoReplyPrint.CP_MultiByteEncoding_UTF8)
            // TestFunction.Test_Pos_SampleTicket_58MM_1(mDcPrint,this)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 10)
            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_HCenter)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextBold(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(mPointer, 0, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintTextInUTF8(mPointer, WString("测试打印"))
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(mPointer, 0, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 10)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextBold(mPointer, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintTextInUTF8(mPointer, WString("一二三四五六七八九十"))
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintTextInUTF8(mPointer, WString("=============================================="))
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_HCenter)
            AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeUnitWidth(mPointer, 6)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintQRCode(mPointer, 0, AutoReplyPrint.CP_QRCodeECC_L, "测试打印二维码")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_Left)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintTextInUTF8(mPointer, WString("12345678910.."))
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_Beep(mPointer, 3, 300)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedAndHalfCutPaper(mPointer)
        }
    }

    fun printOrder(data: PrintOrderBean?) {
        initUsbDevice()
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()

        for (time in 1..times) {
            AutoReplyPrint.INSTANCE.CP_Printer_ClearPrinterBuffer(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_ResetPrinter(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteMode(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteEncoding(mPointer, AutoReplyPrint.CP_MultiByteEncoding_UTF8)
            // TestFunction.Test_Pos_SampleTicket_58MM_1(mDcPrint,this)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 10)
            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_HCenter)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextBold(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(mPointer, 1, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.title)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(mPointer, 0, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 10)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextBold(mPointer, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_Left)

            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "市场名称：${DataApi.mMarketName.isNotAvailable()}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "商户名称：${DataApi.mShopName.isNotAvailable()}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "档口号：${DataApi.mShopStallName.isNotAvailable()}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "订单编号：${data?.orderSn.isNotAvailable()}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "收银时间：${data?.collectionTime.isNotAvailable()}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "操作人：${data?.operationName.isNotAvailable()}")//原本是操作人员，2023年9月14日 17:02:53 渣渣昆叫改成商户名称
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, /*订单金额：*/data?.orderPriceInfo.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "支付信息：${data?.payInfo.isNotAvailable()}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

            if (data?.productList != null && data.productList!!.isNotEmpty()) {
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")
                AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

                //58:384 ; 80:575
                /*表头 1*/
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "品名")
                AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "数量")
                AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 150)
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "单价")
                AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 280)
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "小计")
                AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")
                AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

                data?.productList?.forEach {
                    AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, it.name)
                    AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
                    AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, it.weight)
                    AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 150)
                    AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, it.price)
                    AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 280)
                    AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, it.subPrice)
                    AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
                }

                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")
                AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            }
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.orderTotal.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_HCenter)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.qrCodeTipsTop)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

            if (data?.qrCode != null && data.qrCode!!.isNotBlank()) {
                AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_HCenter)
                AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeUnitWidth(mPointer, 6)
                AutoReplyPrint.INSTANCE.CP_Pos_PrintQRCode(mPointer, 0, AutoReplyPrint.CP_QRCodeECC_L, data?.qrCode)
                AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
                AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_HCenter)
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.qrCodeTipsBottom)
                AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            }
            AutoReplyPrint.INSTANCE.CP_Pos_Beep(mPointer, 3, 300)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedAndHalfCutPaper(mPointer)
        }
    }

//    fun printLastOrder(orderDB: OrderDB?) {
//        initUsbDevice()
//        AutoReplyPrint.INSTANCE.CP_Printer_ClearPrinterBuffer(mPointer)
//        AutoReplyPrint.INSTANCE.CP_Pos_ResetPrinter(mPointer)
//        AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteMode(mPointer)
//        AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteEncoding(mPointer, AutoReplyPrint.CP_MultiByteEncoding_UTF8)
//        // TestFunction.Test_Pos_SampleTicket_58MM_1(mDcPrint,this)
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 10)
//        AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_HCenter)
//        AutoReplyPrint.INSTANCE.CP_Pos_SetTextBold(mPointer, 1)
//        AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(mPointer, 1, 1)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, DataApi.mPrintHeadTips)
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 0)
//        AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(mPointer, 0, 0)
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 10)
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//        AutoReplyPrint.INSTANCE.CP_Pos_SetTextBold(mPointer, 0)
//        AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_Left)
//
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "市场名称：${DataApi.mMarketName}")
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "商户名称：${DataApi.mShopName}")
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "摊位号：${DataApi.mShopStallName}")
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "秤号：${DataApi.mAppUnitId}")
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "溯源码：\n${orderDB?.orderSn}")
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "支付方式：${orderDB?.payTypeName}")
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "交易时间：${orderDB?.payTimeName}")
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//
//        //58:384 ; 80:575
//        /*表头 1*/
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "品名")
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "数量")
//        AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 90)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "单价")
//        AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 280)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "金额(元)")
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//
//        orderDB?.orderProductDBList?.forEach {
//            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, it.productName)
//            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, it.weight)
//            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 90)
//            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, it.oriPrice?.plus("(元/${it.unitName})"))
//            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 280)
//            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, it.subTotalPrice)
//            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//        }
//
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//        AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_Right)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "合计：${orderDB?.totalPrice}元")
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//        AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_HCenter)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, DataApi.mPrintQrTopTips)
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//
//        AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_HCenter)
//        AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeUnitWidth(mPointer, 6)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintQRCode(mPointer, 0, AutoReplyPrint.CP_QRCodeECC_L, DataApi.mViceAwaitOfficialUrlQr)
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//        AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_HCenter)
//        AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, DataApi.mPrintQrBottomTips)
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
//
//        AutoReplyPrint.INSTANCE.CP_Pos_Beep(mPointer, 3, 300)
//        AutoReplyPrint.INSTANCE.CP_Pos_FeedAndHalfCutPaper(mPointer)
//    }

    fun printConnectStatics(data: PrintConnectBean?) {
        initUsbDevice()
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()

        for (time in 1..times) {
            AutoReplyPrint.INSTANCE.CP_Printer_ClearPrinterBuffer(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_ResetPrinter(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteMode(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteEncoding(mPointer, AutoReplyPrint.CP_MultiByteEncoding_UTF8)
            // TestFunction.Test_Pos_SampleTicket_58MM_1(mDcPrint,this)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 10)
            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_HCenter)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextBold(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(mPointer, 1, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.title)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(mPointer, 0, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 10)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextBold(mPointer, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_Left)

            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "收银员：${data?.operationName.isNotAvailable()}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "起始时间：${data?.startTime.isNotAvailable()}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "结束时间：${data?.endTime.isNotAvailable()}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "总单数：")
            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 160)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.billTotal.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "门店：")
            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 160)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.billShop.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "退货：")
            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 160)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.billReturn.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "反结账：")
            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 160)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.billReverse.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "总实收金额：")
            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 160)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.realTotalPrice.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "现金实收：")
            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 160)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.realCashPrice.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "微会员实收：")
            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 160)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.realWetchVipPrice.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "聚合支付实收：")
            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 160)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.realJuHePrice.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "扫码收款实收：")
            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 160)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.realScanQrPrice.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "总销售额：")
            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 160)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.saleTotalPrice.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "现金：")
            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 160)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.saleCashPrice.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "微会员：")
            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 160)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.saleWetchVipPrice.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "聚合支付：")
            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 160)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.saleJuHePrice.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "扫码收款：")
            AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 160)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.saleScanQrPrice.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")

            AutoReplyPrint.INSTANCE.CP_Pos_Beep(mPointer, 3, 300)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedAndHalfCutPaper(mPointer)

        }
    }

    fun printConnectSaleReport(data: PrintConnectSaleReportBean?) {
        initUsbDevice()
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()

        for (time in 1..times) {
            AutoReplyPrint.INSTANCE.CP_Printer_ClearPrinterBuffer(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_ResetPrinter(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteMode(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteEncoding(mPointer, AutoReplyPrint.CP_MultiByteEncoding_UTF8)
            // TestFunction.Test_Pos_SampleTicket_58MM_1(mDcPrint,this)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 10)
            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_HCenter)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextBold(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(mPointer, 1, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.title)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(mPointer, 0, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 10)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextBold(mPointer, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_Left)

            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "收银员：${data?.operationName.isNotAvailable()}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "统计时间：")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "起始 ${data?.startTime.isNotAvailable()}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "结束 ${data?.endTime.isNotAvailable()}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

            if (data?.productList != null && data.productList!!.isNotEmpty()) {
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")
                AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

                //58:384 ; 80:575
                /*表头 1*/
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "品名")
                AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "数量")
                AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 150)
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "单价")
                AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 280)
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "小计")
                AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")
                AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

                data.productList?.forEach {
                    AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, it.name)
                    AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
                    AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, it.weight)
                    AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 150)
                    AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, it.price)
                    AutoReplyPrint.INSTANCE.CP_Pos_SetHorizontalAbsolutePrintPosition(mPointer, 280)
                    AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, it.subPrice)
                    AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
                }

                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")
                AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            }
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, data?.total.isNotAvailable())
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_Beep(mPointer, 3, 300)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedAndHalfCutPaper(mPointer)
        }
    }

    fun printServerCollectOrder(printBean: PrintServerCollectOrderBean) {
        initUsbDevice()
        var times = DataApi.mBillPrintTimesArray[DataApi.mBillPrintTimesIndex].toInt()

        for (time in 1..times) {
            AutoReplyPrint.INSTANCE.CP_Printer_ClearPrinterBuffer(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_ResetPrinter(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteMode(mPointer)
            AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteEncoding(mPointer, AutoReplyPrint.CP_MultiByteEncoding_UTF8)
            // TestFunction.Test_Pos_SampleTicket_58MM_1(mDcPrint,this)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 10)
            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_HCenter)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextBold(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(mPointer, 1, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, printBean.title)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(mPointer, 0, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedDot(mPointer, 10)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextBold(mPointer, 0)
            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(mPointer, AutoReplyPrint.CP_Pos_Alignment_Left)

            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "${printBean.shopName}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "${printBean.startTime}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "${printBean.endTime}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "${printBean.totalPrice}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "${printBean.findZeroPrice}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "${printBean.giftPrice}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "${printBean.realPrice}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "${printBean.orderNumbers}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "-------------------------------")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)

            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "${printBean.cashPrice}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "${printBean.miniProgramPrice}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "${printBean.vipPirce}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "${printBean.juhePrice}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(mPointer, "${printBean.scanQrPrice}")
            AutoReplyPrint.INSTANCE.CP_Pos_FeedLine(mPointer, 1)
            AutoReplyPrint.INSTANCE.CP_Pos_Beep(mPointer, 3, 300)
            AutoReplyPrint.INSTANCE.CP_Pos_FeedAndHalfCutPaper(mPointer)
        }
    }
}